﻿using SBlogCore.Common.App;
using SBlogCore.Entitys.SysUserManager;
using SBlogCore.WebAdminApplication.SysUserManager.Dto;
using SBlogCore.WebAdminApplication.SysUserManager.IServices;

namespace SBlogCore.WebAdminApplication.SysUserManager.Controllers
{
    /// <summary>
    /// 用户管理
    /// </summary>
    public class SysUserManagerController : AuthorizeBaseController
    {
        private readonly ISysUserInfoService _sysUserInfoService;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="sysUserInfoService"></param>
        public SysUserManagerController(ISysUserInfoService sysUserInfoService)
        {
            _sysUserInfoService = sysUserInfoService;
        }
        /// <summary>
        /// 获取列表
        /// </summary>
        /// <param name="inPut"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<PageList<SysUserInfoOutPut>> GetUserList([FromQuery]SysUserInfoQueryInPut inPut)
        {
            var list = await _sysUserInfoService.GetUserList(inPut);
            return list.Adapt<PageList<SysUserInfoOutPut>>(); 
        }
        /// <summary>
        /// 创建用户
        /// </summary>
        /// <param name="inPut"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task AddUser(SysUserInfoInput inPut)
        {
             await _sysUserInfoService.AddUserInfo(inPut);
        }
        /// <summary>
        /// 修改用户
        /// </summary>
        /// <param name="inPut"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task UpdateUser(SysUserInfoEditInput inPut)
        {
             await _sysUserInfoService.UpdateUserInfo(inPut);
        }

        /// <summary>
        /// 账号是否已存在
        /// </summary>
        /// <param name="value"></param>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<bool> AnyAccount(string value, long? id)
        {
            return await _sysUserInfoService.AnyAccount(value, id);
        }

        /// <summary>
        /// 邮箱是否已存在
        /// </summary>
        /// <param name="value"></param>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<bool> AnyEmail(string value,long? id)
        {
            return await _sysUserInfoService.AnyEmail(value, id);
        }
        /// <summary>
        /// 更换我的密码
        /// </summary>
        /// <param name="pass"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task ChangeMyPassWord(string pass)
        {
            var userToken = AppBase.GetJwtUser();
            await _sysUserInfoService.UpdateUserPassWord(userToken.Uid,pass);
        }

        /// <summary>
        /// 获取登录用户信息
        /// </summary>
        /// <returns></returns>
        [HttpGet]

        public async Task<SysUserInfoOutPut> GetLoginUserInfo()
        {
            SysUserInfo info =await _sysUserInfoService.GetLoginUserInfo();
            var res = info.Adapt<SysUserInfoOutPut>();
            res.Roles = new[] { "admin" };
            return res;
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpDelete]
        public async Task DeleteUserInfo(List<long> ids)
        {
            foreach (var id in ids)
            {
                await _sysUserInfoService.DeleteUserInfo(id);
            }
        }

        /// <summary>
        /// 退出登录日后有需要再写逻辑
        /// </summary>
        /// <returns></returns>
        [HttpPost]

        public Task UserLogOut()
        {
            return Task.CompletedTask;
        }
    }
}
